#include "fstream.hpp"

namespace vl
{

file_buf::file_buf(char const* path, int flags)
: f(0)
{
	char const* flagstr = "rb";
	if(flags & out) // TODO: Expand
	{
		if(flags & in)
			flagstr = "r+b";
		else
			flagstr = "wb";
	}
	
	f = fopen(path, flagstr);
}

size_t file_buf::read(byte* p, size_t size)
{
	size_t s = fread(p, 1, size, f);
	if(s == 0)
		return device_buf::eof_value;
	return s;
}

size_t file_buf::write(byte const* p, size_t size)
{
	size_t s = fwrite(p, 1, size, f);
	if(s == 0)
		return device_buf::eof_value;
	return s;
}

void file_buf::do_close()
{
	if(f)
	{
		fclose(f);
		f = 0;
	}
}

file_buf::~file_buf()
{
}

}
